<?php

/**
 * home actions.
 *
 * @package    sf_sandbox
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class homeActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
    }

    public function executeLogin()
    {
        $char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 2));

        // Concatenate the random string onto the random numbers
        // The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
        // '0' is left out to avoid confusion with 'O'
        $str = rand(1, 7) . rand(1, 7) . $char;
        $this->getUser()->setAttribute(Globals::SYSTEM_CAPTCHA_ID, $str);
        
        $c = new Criteria();
        $c->add(TblSettingPeer::F_PARAMETER, Globals::SETTING_SERVER_MAINTAIN);
        $this->tbl_setting = TblSettingPeer::doSelectOne($c);

        //var_dump($this->getUser()->getAttribute(Globals::SYSTEM_CAPTCHA_ID));
    }

    public function executeLogout()
    {
        $this->getUser()->getAttributeHolder()->clear();
        return $this->redirect('home/login');
    }

    public function executeDoLogin()
    {
        $existUser = null;
        if (sfConfig::get('sf_environment') == Globals::SF_ENVIRONMENT_DEV) {
            // ******************* uncomment for testing purpose ****************
            $existUser = TblUserPeer::retrieveByPk(353);
        } else {
            $username = trim($this->getRequestParameter('username'));
            $password = trim($this->getRequestParameter('userpassword'));

            if ($username == '' || $password == '') {
                $this->setFlash('errorMsg', "Invalid username or password.");
                return $this->redirect('home/login');
            }

            $this->getUser()->getAttributeHolder()->clear();

            /*	    user      	*/
            $c = new Criteria();
            $c->add(TblUserPeer::F_USERNAME, $username);
            $c->add(TblUserPeer::F_PASSWORD, $password);
            $c->add(TblUserPeer::F_TYPE, Globals::USERTYPE_DISTRIBUTOR);
            $c->add(TblUserPeer::F_STATUS, Globals::STATUS_ACTIVE);
            $existUser = TblUserPeer::doSelectOne($c);
        }

        if ($existUser) {
            $c = new Criteria();
            $c->add(TblDistributorPeer::F_USER_ID, $existUser->getFId());
            $existDist = TblDistributorPeer::doSelectOne($c);

            $this->getUser()->setAuthenticated(true);
            $this->getUser()->addCredential($existUser->getFType());

            $this->getUser()->setAttribute(Globals::SESSION_DISTID, $existDist->getFId());
            $this->getUser()->setAttribute(Globals::SESSION_USERID, $existUser->getFId());
            $this->getUser()->setAttribute(Globals::SESSION_USERNAME, $existUser->getFUsername());
            $this->getUser()->setAttribute(Globals::SESSION_NICKNAME, $existDist->getFNickname());
            $this->getUser()->setAttribute(Globals::SESSION_USERTYPE, $existUser->getFType());

            $c = new Criteria();
            $c->add(TblSettingPeer::F_PARAMETER, Globals::SETTING_CPS);
            $existSetting = TblSettingPeer::doSelectOne($c);
            //$_SESSION['cps_price'] = $existSetting->getFValue();
            $this->getUser()->setAttribute(Globals::SESSION_CPS_PRICE, $existSetting->getFValue());

            $c = new Criteria();
            $c->add(TblSettingPeer::F_PARAMETER, Globals::SETTING_GOLD_PRICE);
            $existSetting = TblSettingPeer::doSelectOne($c);
            //$this->getUser()->setAttribute(Globals::SESSION_GOLD_PRICE, $existSetting->getFValue());
            $this->getUser()->setAttribute(Globals::SESSION_GOLD_PRICE, 0);

            return $this->redirect('member/summary');
        }

        $this->setFlash('errorMsg', "Invalid username or password.");
        return $this->redirect('home/login');
    }

    public function executeLoginSecurity()
    {
        $this->setFlash('errorMsg', "Login required. This page is not public.");
        return $this->redirect('home/login');
    }

    public function executeLanguage()
    {
        $this->getUser()->setCulture($this->getRequestParameter('lang'));
        $this->redirect($this->getRequest()->getReferer());
    }

    public function executeUpdateMenuIdx()
    {
        $this->getUser()->setAttribute(Globals::SESSION_MENU_IDX, $this->getRequestParameter('menuIdx'));
        return sfView::HEADER_ONLY;
    }

    public function executeLoadDatatableLanguagePack()
    {
        if ($this->getUser()->getCulture() == "cn") {
            echo '{
                "sProcessing":   "处理中...",
                "sLengthMenu":   "显示 _MENU_ 项结果",
                "sZeroRecords":  "没有匹配结果",
                "sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix":  "",
                "sSearch":       "搜索:",
                "sUrl":          "",
                "oPaginate": {
                    "sFirst":    "首页",
                    "sPrevious": "上页",
                    "sNext":     "下页",
                    "sLast":     "末页"
                }
            }';
        } else {
            echo '{}';
        }
        return sfView::HEADER_ONLY;
    }
}
